package org.mobilesynergies.android.epic.service.activities;

import org.mobilesynergies.android.epic.service.ServiceAdministrationActivity;
import org.mobilesynergies.android.epic.service.interfaces.ParameterMap;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class RemoteCommandActivity extends ServiceAdministrationActivity {
	
	private static final String CLASS_TAG = RemoteCommandActivity.class.getName();
	
	public static final String EXTRAS_NODE_FULLADDRESS = "address";
	public static final String EXTRAS_COMMAND_ID = "commandid";
	public static final String EXTRAS_COMMAND_HUMANREADABLENAME = "commandname";
	
	private String mAddress;
	private String mCommand;
	private ParameterMap mCurrentParameterMap = null;
	private String mSessionId = null;

	protected UiGenerator mCurrentUiGenerator;
	private Handler handlerChangeStage = new Handler(){
		
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if(mCurrentParameterMap==null) {
				//TODO change the ui to some error or some finished screen
				return;
			}
			
			mCurrentUiGenerator = new UiGenerator();
			View view = mCurrentUiGenerator.generateUi(RemoteCommandActivity.this, mCurrentParameterMap);
			LinearLayout linearLayoutButtons = new LinearLayout(RemoteCommandActivity.this);
			linearLayoutButtons.setId(1234567890);
			linearLayoutButtons.setOrientation(LinearLayout.HORIZONTAL);

			Button buttonCancel = new Button(RemoteCommandActivity.this);
			buttonCancel.setMinimumWidth(100);
			buttonCancel.setText("Cancel");
			buttonCancel.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					mCurrentParameterMap = null;
					mCurrentUiGenerator = null;
					finish();					
				}
				
			});
			linearLayoutButtons.addView(buttonCancel);
			
			Button buttonOk = new Button(RemoteCommandActivity.this);
			buttonOk.setMinimumWidth(100);
			buttonOk.setText("Ok");
			buttonOk.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					ParameterMap map = null;
					try {
						map = mCurrentUiGenerator.getValues();
						executeCommand(map);
					} catch (Exception e) {
						Toast.makeText(RemoteCommandActivity.this, "An unrecoverable error occured during the execution of this command.", Toast.LENGTH_LONG).show();
						e.printStackTrace();
						finish();
					}
				}
			});
			linearLayoutButtons.addView(buttonOk);
			
			RelativeLayout layoutMain = new RelativeLayout(RemoteCommandActivity.this);
			
			LayoutParams bottomParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		    bottomParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		    layoutMain.addView(linearLayoutButtons, bottomParams);
		    
		    LayoutParams midParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		    midParams.addRule(RelativeLayout.ABOVE, linearLayoutButtons.getId());
		    layoutMain.addView(view, midParams);
		    
			setContentView(layoutMain);						
		}
	};

	
	
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//TODO set contentview to some screen telling the user that the ui is not ready yet
		//maybe also show a progress bar
		Intent callingIntent = getIntent(); 
		if(callingIntent!=null){
			mAddress = callingIntent.getStringExtra(EXTRAS_NODE_FULLADDRESS);
			mCommand = callingIntent.getStringExtra(EXTRAS_COMMAND_ID);
			String name = callingIntent.getStringExtra(EXTRAS_COMMAND_HUMANREADABLENAME);
			this.setTitle(name);
		}
		
	}

	@Override
	protected void onConnected() {
		if(mEpicService.isConnectedToEpicNetwork()){
			executeCommand(null);
		}
	}

	@Override
	protected void onConnectedToEpicNetwork() {
		executeCommand(null);		
	}
	
	private void executeCommand(ParameterMap map){
		mCurrentParameterMap = null;
		mCurrentUiGenerator = null;
		
		if((mAddress==null)||(mCommand==null)){
			return;
		}
		
		try {
			mCurrentParameterMap = new ParameterMap();
			mSessionId  = mEpicService.executeRemoteCommand(mAddress, mCommand, mSessionId, map, mCurrentParameterMap);
			handlerChangeStage.sendEmptyMessage(0);
		} catch (RemoteException e) {
			Toast.makeText(this, "The command could not be executed", Toast.LENGTH_LONG).show();
			//TODO change ui!
			e.printStackTrace();
		}
	}

	@Override
	protected void onDisconnected() {
		// TODO Auto-generated method stub
		
	}

}
